import excuIntervalTry from '@/modules/corejs/excu/excu-interval-try'
let userInfo = null

function getUserInfo (url, { params } = {}) {
  return new Promise(function (resolve, reject) {
    if (userInfo === null) {
      let name = '__cbUserInfo'
      window[name] = function (data) {
        resolve(data)
        delete window[name]
      }
      excuIntervalTry(() => {
        if (window.oa) {
          window.oa.GetUserInfo(name)
          return false
        }
      }, () => {
        reject(new Error('请求超时'))
      })
    } else {
      resolve(userInfo)
    }
  })
}

if (process.env.NODE_ENV !== 'production') {
  /* eslint-disable */
  getUserInfo = function () {
    return new Promise(function (resolve, reject) {
      resolve({
        user_id: '123123'
      })
    })
  }
}

getUserInfo.set = function () {
  userInfo = null
}

export default getUserInfo
